package com.example.blogother.controller;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;

/**
 * @author niuben
 */
@RestController
public class MapController {

    //有百度开放平对生成
    private String ak = "aausOs5E3GaRFUq2fd3oHRSsxQqRw4Bc";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/get_addr")
    public Object getAddrByUserIP(HttpServletRequest request) throws URISyntaxException {
        String ip = request.getRemoteHost();
        System.out.println(ip);
        //如果是内网访问，可以自己手动填写IP地址
        //ip = "xxx.xxx.xxx.xxx";
        //百度地图API调用
        String url = "https://api.map.baidu.com/location/ip?ak="+ak+"&ip="+ip+"&coor=bd09ll";
        //发送请求
        ResponseEntity<Map> forEntity = restTemplate.getForEntity(new URI(url), Map.class);
        Map result = forEntity.getBody();

        return result;
    }

}

